﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DALEF
{
    public class DALEFOperation
    {
        private projectbEntities _connectObject;

        public DALEFOperation(projectbEntities connectionInstance)
        {
            _connectObject = connectionInstance;
          
        }

        public List<DALEF.Department> GetDepartment(DALEF.Department departmentInstance)
        {

            var result = from d in _connectObject.Department
                         where d.DepartmentId == departmentInstance.DepartmentId
                         select d;
          
            return result.ToList();

        }

        public List<DALEF.Department> GetAllDepartment()
        {

            var result = from d in _connectObject.Department
                         select d;


            return result.ToList();
        }

        public void UpdateDepartment(DALEF.Department departmentInstance)
        {
            var result = from d in _connectObject.Department
                         where d.DepartmentId == departmentInstance.DepartmentId
                         select d;
            foreach (var p in result)
            {
                p.DepartmentName = departmentInstance.DepartmentName;
                p.Manager = departmentInstance.Manager;
                p.Phone = departmentInstance.Phone;
                p.Remark = departmentInstance.Remark;
                _connectObject.SaveChanges();
            }
        }

        public void InsertDepartment(DALEF.Department departmentInstance)
        { }

        public void DeleteDepartment(DALEF.Department departmentInstance)
        { }

        public List<DALEF.Teacher> GetAllTeacher()
        {
            var result = from r in _connectObject.Teacher
                         select r;

            return result.ToList();
        }

        public List<DALEF.Teacher> GetTeacher(DALEF.Teacher teacherInstance) 
        {
            var result = from r in _connectObject.Teacher
                         where r.TeacherId == teacherInstance.TeacherId
                         select r;

            return result.ToList();
        }

        public void InsertTeacher(DALEF.Teacher teacherInstance)
        {
            teacherInstance.Department = null;// otherwise the Department table is inserted!!!
            _connectObject.Teacher.AddObject(teacherInstance);
            _connectObject.SaveChanges();
        }

        public void DeleteTeacher(DALEF.Teacher teacherInstance)
        {
            var result = (from r in _connectObject.Teacher
                          where r.TeacherId == teacherInstance.TeacherId
                          select r).ToArray<DALEF.Teacher>();
            foreach (var p in result)
            {
                _connectObject.Teacher.DeleteObject(p);
            }

            _connectObject.SaveChanges(); 
        }

        public void UpdateTeacher(DALEF.Teacher teacherInstance)
        {
            var result = from r in _connectObject.Teacher
                         where r.TeacherId == teacherInstance.TeacherId
                         select r;
            foreach (var p in result)
            {
                p.TeacherName = teacherInstance.TeacherName;
                p.TeacherGender = teacherInstance.TeacherGender;
                p.DepartmentId = teacherInstance.DepartmentId;
                p.Department = new Department()
                {
                    DepartmentId = teacherInstance.Department.DepartmentId,
                    DepartmentName = teacherInstance.Department.DepartmentName,
                    Phone = teacherInstance.Department.Phone,
                    Manager = teacherInstance.Department.Manager,
                    Remark = teacherInstance.Department.Remark

                };

            }
            _connectObject.SaveChanges();
        }
    }
}
